package cn.njyd.corejava.thread.myday04;

import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo01 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(6);
        for (int i=1;i<=20;i++) {
            new Thread(()->{
                try{
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"可以进入泳道");
                    int time = ThreadLocalRandom.current().nextInt(5);
                    TimeUnit.SECONDS.sleep(time);
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName()+"游了"+(time * 10)+"分钟后上岸，还有"+semaphore.availablePermits());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"用户"+i).start();
        }
    }


}
